package cn.chencq.cmd.vm;

import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.chencq.cmd.common.Constants;
import cn.chencq.cmd.util.ShellUtils;

public class MEmuServiceImpl implements MEmuService {

	private static final Logger LOGGER = LoggerFactory.getLogger(MEmuServiceImpl.class);

	@Override
	public boolean createMEmu(String memuName) {
		return false;
	}

	@Override
	public boolean delMEmu(String memuName) {
		Process ps = null;
		boolean ret = false;
		try {
			ps = ShellUtils.cmd(Constants.MEMUHYPERV_ROOT_PATH + "MEmuManage.exe unregistervm \"" + memuName + "\" --delete");
			ret = ps.waitFor() == 0 ? true : false;
		} catch (Exception ex) {
			ex.printStackTrace();
			LOGGER.error("delMEmu error,throw exception:" + ex);
			ret = false;
		} finally {
			if (ps != null) {
				ps.destroy();
			}
		}
		return ret;
	}

	@Override
	public boolean cloneMEmu(String memuName) {
		Process ps = null;
		boolean ret = false;
		try {
			ps = ShellUtils.cmd(Constants.MEMU_ROOT_PATH + "MEmuConsole.exe  clone \"" + memuName + "\"");
			ret = ps.waitFor() == 0 ? true : false;
		} catch (Exception ex) {
			ex.printStackTrace();
			LOGGER.error("cloneMEmu error,throw exception:" + ex);
			ret = false;
		} finally {
			if (ps != null) {
				ps.destroy();
			}
		}
		return ret;
	}

	@Override
	public boolean closeMEmu(String memuName) {
		ShellUtils.cmd(Constants.MEMUHYPERV_ROOT_PATH + "MEmuManage.exe controlvm \"" + memuName + "\" poweroff");
		ShellUtils.cmd("taskkill /f /im MEmu.exe");
		ShellUtils.cmd("taskkill /f /im MEmuHeadless.exe");
		return true;
	}

	@Override
	public boolean startMEmu(String memuName) {
		Process ps = null;
		boolean ret = false;
		try {
			ps = ShellUtils.cmd(Constants.MEMU_ROOT_PATH + "MEmuConsole.exe \"" + memuName + "\"");
			ret = ps.waitFor() == 0 ? true : false;
		} catch (Exception ex) {
			ex.printStackTrace();
			LOGGER.error("startMEmu error,throw exception:" + ex);
			ret = false;
		} finally {
			if (ps != null) {
				ps.destroy();
			}
		}
		return ret;
	}

	@Override
	public Set<String> getMEmuList() {
		Set<String> mSet = new HashSet<String>();
		Process ps = null;
		try {
			ps = ShellUtils.cmd(Constants.MEMUHYPERV_ROOT_PATH + "MEmuManage.exe list runningvms");
			String str = ShellUtils.getShellOut(ps);
			Pattern p = Pattern.compile("\"([^\"]*)\"");
			Matcher m = p.matcher(str);
			while (m.find()) {
				mSet.add(m.group(1));
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			LOGGER.error("getMEmuList error,throw exception:" + ex);
		} finally {
			if (ps != null) {
				ps.destroy();
			}
		}
		return mSet;
	}

	@Override
	public boolean setGuestProperty(String memuName, String key, String value) {
		Process ps = null;
		boolean ret = false;
		try {
			ps = ShellUtils.cmd(Constants.MEMUHYPERV_ROOT_PATH + "MEmuManage.exe guestproperty  set \"" + memuName + "\" \"" + key + "\" \"" + value + "\"");
			ret = ps.waitFor() == 0 ? true : false;
		} catch (Exception ex) {
			ex.printStackTrace();
			LOGGER.error("setGuestProperty error,throw exception:" + ex);
			ret = false;
		} finally {
			if (ps != null) {
				ps.destroy();
			}
		}
		return ret;
	}

}
